#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import json
import re
from xtls.basecrawler import BaseCrawler
from xtls.logger import get_logger

from company_xsb_brief_handler import BriefHandler
from company_xsb_management_handler import ManagementHandler
from company_xsb_holder_handler import HolderHandler


def str_json(html):
    json_str = re.findall('\((.+)\)', html)[0]
    json_data = json.loads(json_str)
    return json_data or dict()

class CompanyXsb(BaseCrawler):
    def __init__(self, stock_list):
        super(CompanyXsb, self).__init__()
        self.logger = get_logger(__file__)
        self.stock_list = stock_list
        self.unfinished_id = set()
        self.url_format = 'http://www.neeq.com.cn/nqhqController/detailCompany.do?callback=jQuery183090558209316805_1468296293179&zqdm={}&_=1468303895153'

    def parse(self, stock):
        stock_code = stock.stock_code
        url = self.url_format.format(stock_code)
        html = self.get(url)
        json_content = str_json(html)
        return json_content

    def run(self):
        total = 0
        for stock in self.stock_list:
            total += 1
            data = self.parse(stock)

            brief_handler = BriefHandler(stock, data.get('baseinfo', dict()), stock.brief_op)
            brief_handler.run()
            self.unfinished_id.union(brief_handler.get_unfinished_id())

            management_handler = ManagementHandler(stock, data.get('executives', dict()), stock.management_op)
            management_handler.run()
            self.unfinished_id.union(management_handler.get_unfinished_id())

            holder_handler = HolderHandler(stock, data.get('topTenHolders', dict()), stock.holder_op)
            holder_handler.run()
            self.unfinished_id.union(holder_handler.get_unfinished_id())

            self.logger.info('[%d] Completed %s' % (total, stock.stock_code))

    def get_unfinished_id(self):
        return self.unfinished_id


